package com.boyu.backend;

import com.boyu.common.BaseController;
import com.boyu.common.OptionConst;
import com.boyu.common.model.Option;
import com.boyu.common.model.SystemAttr;

import java.util.List;

/**
 * _OptionController
 */
public class _SystemAttrController extends BaseController {
    protected int getGroupId() {
        return OptionConst.SYSTEM_ATTR_GROUP;
    }

    public void index() {
        List<Option> options = Option.dao.find("select * from t_option where groupId = " + getGroupId());
        List<SystemAttr> systemAttrList = SystemAttr.dao.find("select * from t_system_attr");
        setAttr("optionList",options);
        setAttr("systemAttrList",systemAttrList);
        render(INDEX_HTML);
    }

    public void add() {
        int optionId = getParaToInt("optionId",-1);
        List<Option> options = Option.dao.find("select * from t_option where groupId = " + getGroupId());
        setAttr("optionList",options);
        setAttr("optionId",optionId);
        render(ADD_HTML);
    }

    public void edit() {
        List<Option> options = Option.dao.find("select * from t_option where groupId = " + getGroupId());
        setAttr("optionList",options);
        long id = getParaToLong(ID);
        SystemAttr byId = SystemAttr.dao.findById(id);
        setAttr("model", byId);
        setAttr("optionId",byId.getOptionId());
        render(EDIT_HTML);
    }

    public void save() {
        SystemAttr model = getModel(SystemAttr.class, "");
        if (model.getId() != null) {
            model.update();
        } else {
            model.save();
        }
        redirect(INDEX);
    }
    public void del() {
        SystemAttr.dao.deleteById(getParId());
        redirect(INDEX);
    }
}





